package com.example.comic.mvp.view

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.support.v4.app.ActivityOptionsCompat
import android.support.v4.app.Fragment
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.comic.R
import com.example.comic.USE_URL
import com.example.comic.adpter.BookAdapter
import com.example.comic.mvp.model.Book
import com.example.comic.mvp.ppresenter.BookPresenter
import kotlinx.android.synthetic.main.item_book.view.*
import org.jetbrains.anko.async
import org.jetbrains.anko.uiThread

class RankFragment : Fragment() {
    companion object {
        val AIM_URL: String = USE_URL().RANK_TITLE_URL
    }

    var mData = ArrayList<Book>()
    lateinit var adapter: BookAdapter
    lateinit var recyclerView: RecyclerView
    lateinit var swipeRefreshLayout: SwipeRefreshLayout
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.retainInstance = true
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_book, container, false)
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
    }

    private fun initView(view: View) {
        swipeRefreshLayout = view.findViewById(R.id.swipeRefresh)
        recyclerView = view.findViewById(R.id.recyclerView)
        recyclerView.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        adapter = BookAdapter { itemView: View, i: Int -> jumpToChapter(itemView, i) }
        recyclerView.adapter = adapter
        swipeRefreshLayout.setOnRefreshListener { load() }
        swipeRefreshLayout.post { swipeRefreshLayout.isRefreshing = true }
    }

    private fun jumpToChapter(itemView: View, position: Int) {
        val intent = Intent(context, ChapterActivity::class.java)
        intent.putExtra(ChapterActivity.INTENT_LINK, mData[position].link)
        intent.putExtra(ChapterActivity.INTENT_COVER, mData[position].cover)
        intent.putExtra(ChapterActivity.INTENT_TITLE, mData[position].title)
        intent.putExtra(ChapterActivity.INTENT_CATEGORY, mData[position].category)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val option: ActivityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(this!!.activity!!, itemView.ivCover, "imageView")
            startActivity(intent, option.toBundle())
        } else
            startActivity(intent)
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser && mData.size == 0)
            load()
    }

    private fun load() {
        async {
            val data = BookPresenter().rankTitle(AIM_URL)
            uiThread {
                mData = data
                adapter.refreshData(mData)
                swipeRefreshLayout.isRefreshing = false
            }
        }
    }
}